<?php
declare (strict_types=1);

namespace app\admin\controller\base;

use app\admin\service\base\MenuService;
use app\BaseController;
use think\facade\Request;
use think\response\Json;

/**
 * 菜单权限管理
 */
class Menu extends BaseController
{

    /**
     * 获取菜单目录
     * @param MenuService $menuService
     * @return Json
     */
    public function authRouters(MenuService $menuService): Json
    {
        return $menuService->authRouters();
    }

    /**
     * 菜单管理树状图
     * @param MenuService $menuService
     * @return Json
     */
    public function menuTree(MenuService $menuService): Json
    {
        $role_id = Request::param('role_id/d', 0);
        return $menuService->menuTree($role_id);
    }

    /**
     * 菜单管理列表
     * @param MenuService $menuService
     * @return Json
     */
    public function menuList(MenuService $menuService): Json
    {
        return $menuService->menuList();
    }

    /**
     * 菜单管理添加
     * @param MenuService $menuService
     * @return Json
     */
    public function menuAdd(MenuService $menuService): Json
    {
        $params['parent_id'] = Request::param('parent_id/d', 0);
        $params['menu_name'] = Request::param('menu_name/s', '');
        $params['menu_type'] = Request::param('menu_type/s', 'M');
        $params['orderby'] = Request::param('orderby/d', 0);
        $params['path'] = Request::param('path/s', '');
        $params['component'] = Request::param('component/s', 'Layout');
        $params['query'] = Request::param('query/s', '');
        $params['rule'] = Request::param('rule/s', '');
        $params['is_frame'] = Request::param('is_frame/d', 0);
        $params['status'] = Request::param('status/d', 1);
        $params['visible'] = Request::param('visible/d', 1);
        $params['icon'] = Request::param('icon/s', '');
        $params['remark'] = Request::param('remark/s', '');
        return $menuService->menuAdd($params);
    }

    /**
     * 菜单管理编辑
     * @param MenuService $menuService
     * @return Json
     */
    public function menuEdit(MenuService $menuService): Json
    {
        $params['id'] = Request::param('id/d', 0);
        $params['parent_id'] = Request::param('parent_id/d', 0);
        $params['menu_name'] = Request::param('menu_name/s', '');
        $params['menu_type'] = Request::param('menu_type/s', 'M');
        $params['orderby'] = Request::param('orderby/d', 0);
        $params['path'] = Request::param('path/s', '');
        $params['component'] = Request::param('component/s', 'Layout');
        $params['query'] = Request::param('query/s', '');
        $params['rule'] = Request::param('rule/s', '');
        $params['is_frame'] = Request::param('is_frame/d', 0);
        $params['status'] = Request::param('status/d', 1);
        $params['visible'] = Request::param('visible/d', 1);
        $params['icon'] = Request::param('icon/s', '');
        $params['remark'] = Request::param('remark/s', '');
        return $menuService->menuEdit($params);
    }

    /**
     * 菜单管理删除
     * @param MenuService $menuService
     * @return Json
     */
    public function menuDel(MenuService $menuService): Json
    {
        $params['id'] = Request::param('id', 0);
        return $menuService->menuDel($params);
    }

    /**************************** 角色管理 ********************************/
    /**
     * 角色管理列表
     * @param MenuService $menuService
     * @return Json
     */
    public function roleList(MenuService $menuService): Json
    {
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $params['role_name'] = Request::param('role_name/s');
        $params['role_key'] = Request::param('role_key/s');
        $params['status'] = Request::param('status/d');
        $params['start_time'] = Request::param('start_time/d', 0);
        $params['end_time'] = Request::param('end_time/d', 0);
        return $menuService->roleList($params);
    }

    /**
     * 角色管理全部列表
     * @param MenuService $menuService
     * @return Json
     */
    public function roleListAll(MenuService $menuService): Json
    {
        $params['role_name'] = Request::param('role_name/s');
        return $menuService->roleListAll($params);
    }

    /**
     * 角色管理添加
     * @param MenuService $menuService
     * @return Json
     */
    public function roleAdd(MenuService $menuService): Json
    {
        $params['role_name'] = Request::param('role_name/s', '');
        $params['role_key'] = Request::param('role_key/s', '');
        $params['orderby'] = Request::param('orderby/d', 0);
        $params['status'] = Request::param('status/d', 1);
        $params['menu_ids'] = Request::param('menu_ids/a', []);
        $params['remark'] = Request::param('remark/s', '');
        return $menuService->roleAdd($params);
    }

    /**
     * 角色管理编辑
     * @param MenuService $menuService
     * @return Json
     */
    public function roleEdit(MenuService $menuService): Json
    {
        $params['id'] = Request::param('id/d', 0);
        $params['role_name'] = Request::param('role_name/s', '');
        $params['role_key'] = Request::param('role_key/s', '');
        $params['orderby'] = Request::param('orderby/d', 0);
        $params['status'] = Request::param('status/d', 1);
        $params['menu_ids'] = Request::param('menu_ids/a', []);
        $params['remark'] = Request::param('remark/s', '');
        return $menuService->roleEdit($params);
    }

    /**
     * 角色管理删除
     * @param MenuService $menuService
     * @return Json
     */
    public function roleDel(MenuService $menuService): Json
    {
        $params['id'] = Request::param('id', 0);
        return $menuService->roleDel($params);
    }

    /**
     * 角色管理状态
     * @param MenuService $menuService
     * @return Json
     */
    public function roleStatus(MenuService $menuService): Json
    {
        $params['id'] = Request::param('id/d', 0);
        $params['status'] = Request::param('status/d', 2);
        return $menuService->roleStatus($params['id'], $params['status']);
    }

    /**************************** 角色分配用户管理 ***********************/
    /**
     * 角色已分配用户列表
     * @param MenuService $menuService
     * @return Json
     */
    public function roleAssignedList(MenuService $menuService): Json
    {
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $params['id'] = Request::param('id/d', 0);
        $params['name'] = Request::param('name/s');
        $params['phone'] = Request::param('phone/s');
        return $menuService->roleAssignedList($params);
    }

    /**
     * 角色未分配用户列表
     * @param MenuService $menuService
     * @return Json
     */
    public function roleUnassignedList(MenuService $menuService): Json
    {
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $params['id'] = Request::param('id/d', 0);
        $params['name'] = Request::param('name/s');
        $params['phone'] = Request::param('phone/s');
        return $menuService->roleUnassignedList($params);
    }

    /**
     * 角色未分配用户授权
     * @param MenuService $menuService
     * @return Json
     */
    public function roleAuthorization(MenuService $menuService): Json
    {
        $params['id'] = Request::param('id/d', 0);
        $params['admin_ids'] = Request::param('admin_ids/a', []);
        return $menuService->roleAuthorization($params);
    }

    /**
     * 角色已分配用户取消授权
     * @param MenuService $menuService
     * @return Json
     */
    public function roleUnAuthorization(MenuService $menuService): Json
    {
        $params['id'] = Request::param('id/d', 0);
        $params['admin_ids'] = Request::param('admin_ids/a', []);
        return $menuService->roleUnAuthorization($params);
    }


}
